package com.culturalCenter.dataCenter.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.culturalCenter.dataCenter.Utils.HttpClientPoolUtil;
import com.culturalCenter.dataCenter.Utils.RSAUtils;
import com.culturalCenter.dataCenter.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>Title:SdkClient</p>
 * <p>Description:客户端调用工具类</p>
 * <p>Company:浙江大华技术股份有限公司</p>
 *
 * @author 32174
 * @date 2018年12月18日
 */
@Slf4j
public class SdkClient {

    static {
        try {
            Security.addProvider(new BouncyCastleProvider());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * @Title:get
     * @Description:get请求
     * @param:@param url
     * @param:@param headMap
     * @param:@return
     * @param:@throws Exception
     * @return:String
     */
    public static String get(String url, Map<String, String> headMap) throws Exception {
        return HttpClientPoolUtil.get(url, headMap);
    }

    /**
     * @Title:post
     * @Description:post请求
     * @param:@param url
     * @param:@param data JSON序列化后的参数 JSON.toJSONString(xxx)
     * @param:@param headMap
     * @param:@return
     * @param:@throws Exception
     * @return:String
     */
    public static String post(String url, String data, Map<String, String> headMap) throws Exception {
        return HttpClientPoolUtil.post(url, data, headMap);
    }

    /**
     * @Title:put
     * @Description:put请求
     * @param:@param url
     * @param:@param data JSON序列化后的参数 JSON.toJSONString(xxx)
     * @param:@param headMap
     * @param:@return
     * @param:@throws Exception
     * @return:String
     */
    public static String put(String url, String data, Map<String, String> headMap) throws Exception {
        return HttpClientPoolUtil.put(url, data, headMap);
    }

    /**
     * @Title:delete
     * @Description:delete请求
     * @param:@param url
     * @param:@param data JSON序列化后的参数 JSON.toJSONString(xxx)
     * @param:@param headMap
     * @param:@return
     * @param:@throws Exception
     * @return:String
     */
    public static String delete(String url, String data, Map<String, String> headMap) throws Exception {
        return HttpClientPoolUtil.delete(url, headMap);
    }

    /**
     * @Title:login
     * @Description:模拟登录调用,获取token
     * @param:@param ip
     * @param:@param port
     * @param:@param userName  用户名              用户名及密码由大华区域技术提供
     * @param:@param password 密码(明文)
     * @param:@param headMap
     * @param:@return
     * @param:@throws Exception
     * @return:String
     */
    public static String login(String ip, String port, String userName, String password, Map<String, String> headMap) throws Exception {
        String result = "";
        String publicKeyUrl = MessageFormat.format(Constants.GET_PUBLIC_KEY, ip, port);
        String loginUrl = MessageFormat.format(Constants.LOGIN, ip, port);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("loginName", userName);
        String publicKeyResult = HttpClientPoolUtil.post(publicKeyUrl, JSON.toJSONString(paramMap), headMap);
        if ((publicKeyResult != null && !"".equals(publicKeyResult))) {
            JSONObject publicKeyResultObject = JSON.parseObject(publicKeyResult);
            String publicKey = publicKeyResultObject.getString("publicKey");
            if (publicKey == null || "".equals(publicKey)) {
                throw new Exception("获取token失败");
            }
            String entryPassword = RSAUtils.encryptBASE64(RSAUtils.encryptByPublicKey(password.getBytes(), publicKey));
            paramMap.put("loginPass", entryPassword);
            result = HttpClientPoolUtil.post(loginUrl, JSON.toJSONString(paramMap), headMap);
        }
        return result;
    }

}